package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.ToastShowUtil;

import butterknife.BindView;

public class WritePlActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_fb)
    TextView tvFb;
    @BindView(R.id.rb_pl_star)
    RatingBar rbStar;
    @BindView(R.id.tv_pl_star)
    TextView tvPlStar;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_count)
    TextView tvCount;

    private int plStar;
    private int BOOK_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_write_pl);
    }

    @Override
    protected void initData() {
        plStar = 0;
        initIntent();
        initClick();
    }

    private void initIntent() {
        Intent intent = getIntent();
        BOOK_ID = intent.getIntExtra("BOOK_ID", -1);
    }

    private void initClick() {
        rbStar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                plStar = (int) v;
                if (v >= 8) {
                    tvPlStar.setText("好");
                } else if (v >= 5 && v < 8) {
                    tvPlStar.setText("正常");
                } else {
                    tvPlStar.setText("差");
                }
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                try {
                    if (charSequence.toString().length() > 800) {
                        etContent.setText(charSequence.toString().substring(0, 800));
                        ToastShowUtil.showToastCenter(WritePlActivity.this, "超出最大字数限制");
                    } else {
                        tvCount.setText(charSequence.toString().length() + "/800");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvFb.setOnClickListener(view -> {
            if (plStar == 0.0) {
                ToastShowUtil.showToastCenter(WritePlActivity.this, "请选择星级");
            } else if (BusinessUtils.isEmpty(etContent.getText().toString())) {
                ToastShowUtil.showToastCenter(WritePlActivity.this, "分享你对这本书的看法");
            } else {
                submitPl();
            }
        });
    }

    private void submitPl() {
        mPresenter.plBook(WritePlActivity.this, plStar, BOOK_ID, etContent.getText().toString(), true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        ToastShowUtil.showToastCenter(WritePlActivity.this, "评论成功，等待后台审核");
                        finish();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(WritePlActivity.this, msg);
                        break;
                }
            }
        });
    }
}